package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.Manholes;
import com.ruoyi.system.mapper.CablesMapper;
import com.ruoyi.system.domain.Cables;
import com.ruoyi.system.service.ICablesService;

/**
 * 光缆表Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-13
 */
@Service
public class CablesServiceImpl implements ICablesService 
{
    @Autowired
    private CablesMapper cablesMapper;

    /**
     * 查询光缆表
     * 
     * @param id 光缆表主键
     * @return 光缆表
     */
    @Override
    public Cables selectCablesById(Long id)
    {
        return cablesMapper.selectCablesById(id);
    }

    /**
     * 查询光缆表列表
     * 
     * @param cables 光缆表
     * @return 光缆表
     */
    @Override
    public List<Cables> selectCablesList(Cables cables)
    {
        return cablesMapper.selectCablesList(cables);
    }

    /**
     * 新增光缆表
     * 
     * @param cables 光缆表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCables(Cables cables)
    {
        int rows = cablesMapper.insertCables(cables);
        insertManholes(cables);
        return rows;
    }

    /**
     * 修改光缆表
     * 
     * @param cables 光缆表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCables(Cables cables)
    {
        cablesMapper.deleteManholesByCableId(cables.getId());
        insertManholes(cables);
        return cablesMapper.updateCables(cables);
    }

    /**
     * 批量删除光缆表
     * 
     * @param ids 需要删除的光缆表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCablesByIds(Long[] ids)
    {
        cablesMapper.deleteManholesByCableIds(ids);
        return cablesMapper.deleteCablesByIds(ids);
    }

    /**
     * 删除光缆表信息
     * 
     * @param id 光缆表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCablesById(Long id)
    {
        cablesMapper.deleteManholesByCableId(id);
        return cablesMapper.deleteCablesById(id);
    }

    /**
     * 新增炮筒表信息
     * 
     * @param cables 光缆表对象
     */
    public void insertManholes(Cables cables)
    {
        List<Manholes> manholesList = cables.getManholesList();
        Long id = cables.getId();
        if (StringUtils.isNotNull(manholesList))
        {
            List<Manholes> list = new ArrayList<Manholes>();
            for (Manholes manholes : manholesList)
            {
                manholes.setCableId(id);
                list.add(manholes);
            }
            if (list.size() > 0)
            {
                cablesMapper.batchManholes(list);
            }
        }
    }
}
